home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-aux / book.aux / nurbs.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  6KB  |  220 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  nurbs.c
  39.  *  This program shows a NURBS (Non-uniform rational B-splines)
  40.  *  surface, shaped like a heart.
  41.  */
  42. #include <GL/gl.h>
  43. #include <GL/glu.h>
  44. #include <stdlib.h>
  45. #include <GL/glaux.h>
  46.  
  47. #define S_NUMPOINTS 13
  48. #define S_ORDER     3
  49. #define S_NUMKNOTS  (S_NUMPOINTS + S_ORDER)
  50. #define T_NUMPOINTS 3
  51. #define T_ORDER     3
  52. #define T_NUMKNOTS  (T_NUMPOINTS + T_ORDER)
  53. #define SQRT2    1.41421356237309504880
  54.  
  55. /*
  56.  * initialized local data 
  57.  */
  58.  
  59. GLfloat sknots[S_NUMKNOTS] =
  60. {-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0,
  61.  4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 9.0, 9.0};
  62. GLfloat tknots[T_NUMKNOTS] =
  63. {1.0, 1.0, 1.0, 2.0, 2.0, 2.0};
  64.  
  65. GLfloat ctlpoints[S_NUMPOINTS][T_NUMPOINTS][4] =
  66. {
  67.   {
  68.     {4., 2., 2., 1.},
  69.     {4., 1.6, 2.5, 1.},
  70.     {4., 2., 3.0, 1.}},
  71.   {
  72.     {5., 4., 2., 1.},
  73.     {5., 4., 2.5, 1.},
  74.     {5., 4., 3.0, 1.}},
  75.   {
  76.     {6., 5., 2., 1.},
  77.     {6., 5., 2.5, 1.},
  78.     {6., 5., 3.0, 1.}},
  79.   {
  80.     {SQRT2 * 6., SQRT2 * 6., SQRT2 * 2., SQRT2},
  81.     {SQRT2 * 6., SQRT2 * 6., SQRT2 * 2.5, SQRT2},
  82.     {SQRT2 * 6., SQRT2 * 6., SQRT2 * 3.0, SQRT2}},
  83.   {
  84.     {5.2, 6.7, 2., 1.},
  85.     {5.2, 6.7, 2.5, 1.},
  86.     {5.2, 6.7, 3.0, 1.}},
  87.   {
  88.     {SQRT2 * 4., SQRT2 * 6., SQRT2 * 2., SQRT2},
  89.     {SQRT2 * 4., SQRT2 * 6., SQRT2 * 2.5, SQRT2},
  90.     {SQRT2 * 4., SQRT2 * 6., SQRT2 * 3.0, SQRT2}},
  91.   {
  92.     {4., 5.2, 2., 1.},
  93.     {4., 4.6, 2.5, 1.},
  94.     {4., 5.2, 3.0, 1.}},
  95.   {
  96.     {SQRT2 * 4., SQRT2 * 6., SQRT2 * 2., SQRT2},
  97.     {SQRT2 * 4., SQRT2 * 6., SQRT2 * 2.5, SQRT2},
  98.     {SQRT2 * 4., SQRT2 * 6., SQRT2 * 3.0, SQRT2}},
  99.   {
  100.     {2.8, 6.7, 2., 1.},
  101.     {2.8, 6.7, 2.5, 1.},
  102.     {2.8, 6.7, 3.0, 1.}},
  103.   {
  104.     {SQRT2 * 2., SQRT2 * 6., SQRT2 * 2., SQRT2},
  105.     {SQRT2 * 2., SQRT2 * 6., SQRT2 * 2.5, SQRT2},
  106.     {SQRT2 * 2., SQRT2 * 6., SQRT2 * 3.0, SQRT2}},
  107.   {
  108.     {2., 5., 2., 1.},
  109.     {2., 5., 2.5, 1.},
  110.     {2., 5., 3.0, 1.}},
  111.   {
  112.     {3., 4., 2., 1.},
  113.     {3., 4., 2.5, 1.},
  114.     {3., 4., 3.0, 1.}},
  115.   {
  116.     {4., 2., 2., 1.},
  117.     {4., 1.6, 2.5, 1.},
  118.     {4., 2., 3.0, 1.}}
  119. };
  120.  
  121. GLUnurbsObj *theNurb;
  122.  
  123. /*
  124.  * Initialize material property, light source, lighting model, 
  125.  * *  and depth buffer.
  126.  */
  127. void myinit(void)
  128. {
  129.   GLfloat mat_ambient[] =
  130.   {1.0, 1.0, 1.0, 1.0};
  131.   GLfloat mat_diffuse[] =
  132.   {1.0, 0.2, 1.0, 1.0};
  133.   GLfloat mat_specular[] =
  134.   {1.0, 1.0, 1.0, 1.0};
  135.   GLfloat mat_shininess[] =
  136.   {50.0};
  137.  
  138.   GLfloat light0_position[] =
  139.   {1.0, 0.1, 1.0, 0.0};
  140.   GLfloat light1_position[] =
  141.   {-1.0, 0.1, 1.0, 0.0};
  142.  
  143.   GLfloat lmodel_ambient[] =
  144.   {0.3, 0.3, 0.3, 1.0};
  145.  
  146.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  147.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  148.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  149.   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  150.   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  151.   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  152.   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  153.  
  154.   glEnable(GL_LIGHTING);
  155.   glEnable(GL_LIGHT0);
  156.   glEnable(GL_LIGHT1);
  157.   glDepthFunc(GL_LESS);
  158.   glEnable(GL_DEPTH_TEST);
  159.   glEnable(GL_AUTO_NORMAL);
  160.  
  161.   theNurb = gluNewNurbsRenderer();
  162.  
  163.   gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
  164.   gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
  165. }
  166.  
  167. void display(void)
  168. {
  169.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  170.  
  171.   glPushMatrix();
  172.   glTranslatef(4., 4.5, 2.5);
  173.   glRotatef(220.0, 1., 0., 0.);
  174.   glRotatef(115.0, 0., 1., 0.);
  175.   glTranslatef(-4., -4.5, -2.5);
  176.  
  177.   gluBeginSurface(theNurb);
  178.   gluNurbsSurface(theNurb,
  179.           S_NUMKNOTS, sknots,
  180.           T_NUMKNOTS, tknots,
  181.           4 * T_NUMPOINTS,
  182.           4,
  183.           &ctlpoints[0][0][0],
  184.           S_ORDER, T_ORDER,
  185.           GL_MAP2_VERTEX_4);
  186.   gluEndSurface(theNurb);
  187.  
  188.   glPopMatrix();
  189.   glFlush();
  190. }
  191.  
  192. void myReshape(int w, int h)
  193. {
  194.   glViewport(0, 0, w, h);
  195.   glMatrixMode(GL_PROJECTION);
  196.   glLoadIdentity();
  197.   glFrustum(-2.0, 2.0, -2.0, 2.0, 0.8, 10.0);
  198.  
  199.   glMatrixMode(GL_MODELVIEW);
  200.   glLoadIdentity();
  201.   gluLookAt(7.0, 4.5, 4.0, 4.5, 4.5, 2.0, 6.0, -3.0, 2.0);
  202. }
  203.  
  204. /*
  205.  * Main Loop
  206.  * *  Open window with initial window size, title bar, 
  207.  * *  RGBA display mode, and handle input events.
  208.  */
  209. int main(int argc, char **argv)
  210. {
  211.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  212.   auxInitPosition(0, 0, 500, 500);
  213.   if (!auxInitWindow(argv[0]))
  214.     auxQuit();
  215.   myinit();
  216.   auxReshapeFunc(myReshape);
  217.   auxMainLoop(display);
  218.   return 0;
  219. }
  220.